package com.jrx.anytxn.investor.timer.job.queryCreditResult.step;

import com.jrx.anytxn.investor.bean.InvestorQueryCreditResultBean;
import com.jrx.anytxn.investor.entity.TlCustomerInvestorLimit;
import com.jrx.anytxn.investor.entity.TlInvestorLimitApproveLog;
import com.jrx.anytxn.investor.mapper.ext.ExtTlCustomerInvestorLimitMapper;
import com.jrx.anytxn.investor.mapper.ext.ExtTlInvestorLimitApproveLogMapper;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemWriter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 查询合作行授信结果 - writer
 *
 * @author liuyang
 * @date 2020-06-09
 */
@Service
public class InvestorQueryCreditResultWriter implements ItemWriter<InvestorQueryCreditResultBean> {

    private static final Logger logger = LoggerFactory.getLogger(InvestorQueryCreditResultWriter.class);

    @Resource
    private ExtTlInvestorLimitApproveLogMapper investorLimitApproveLogMapper;

    @Resource
    private ExtTlCustomerInvestorLimitMapper customerInvestorLimitMapper;

    public void write(List items) {

        List<TlInvestorLimitApproveLog> updateInvestorLimitApproveLogs = new ArrayList<>();

        List<TlCustomerInvestorLimit> customerInvestorLimits = new ArrayList<>();

        if (CollectionUtils.isNotEmpty(items)) {
            for (Object obj : items) {
                InvestorQueryCreditResultBean bean = (InvestorQueryCreditResultBean) obj;
                if (bean.getUpdateInvestorLimitApproveLog()!=null) {
                    updateInvestorLimitApproveLogs.add(bean.getUpdateInvestorLimitApproveLog());
                }
                if (bean.getCustomerInvestorLimit()!=null) {
                    customerInvestorLimits.add(bean.getCustomerInvestorLimit());
                }
            }
        }

        if (CollectionUtils.isNotEmpty(updateInvestorLimitApproveLogs)) {
            investorLimitApproveLogMapper.batchUpdateForBatch(updateInvestorLimitApproveLogs);
        }

        if (CollectionUtils.isNotEmpty(customerInvestorLimits)) {
            customerInvestorLimitMapper.batchInsert(customerInvestorLimits);
        }
    }
}
